package com.example.admin.goodnews.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.support.v7.widget.RecyclerView;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.admin.goodnews.R;
import com.example.admin.goodnews.entity.TuPianDataEntity;
import com.example.admin.goodnews.interfaces.RecyclerViewItemClickListener;
import com.example.admin.goodnews.interfaces.RecyclerViewItemLongClickListener;
import com.squareup.picasso.Picasso;

import java.util.List;

/**
 * 轻松一下图片适配器
 * Created by admin on 2016/10/14.
 */

public class TuPianAdapter extends RecyclerView.Adapter<TuPianAdapter.ViewHolder> {
    private Context context;
    private List<TuPianDataEntity.ShowapiResBodyBean.PagebeanBean.ContentlistBean> datalists;

    public TuPianAdapter(Context context, List<TuPianDataEntity.ShowapiResBodyBean.PagebeanBean.ContentlistBean> datalists) {
        this.context = context;
        this.datalists = datalists;
    }

    private RecyclerViewItemClickListener recyclerViewItemClickListener;
    private RecyclerViewItemLongClickListener recyclerViewItemLongClickListener;

    public void setRecyclerViewItemClickListener(RecyclerViewItemClickListener recyclerViewItemClickListener) {
        this.recyclerViewItemClickListener = recyclerViewItemClickListener;
    }

    public void setRecyclerViewItemLongClickListener(RecyclerViewItemLongClickListener recyclerViewItemLongClickListener) {
        this.recyclerViewItemLongClickListener = recyclerViewItemLongClickListener;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_tupian, null);
        ViewHolder holder = new ViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, int position) {
        String text = datalists.get(position).getText();
        // 去除文本中的空格换行符等内容
        text.replaceAll("\\s", "");
        text.replaceAll("\n", "");
        text.replaceAll("\r", "");
        text.replaceAll("\t", "");
        text.replaceAll("\t1", "");
        text.replaceAll("\t2", "");
        text.replaceAll("\t3", "");
        text.replaceAll("\4", "");
        text.replaceAll("\t5", "");
        holder.tv_tupian_title.setText(Html.fromHtml(text).toString());
        Picasso.with(context).load(datalists.get(position).getImage0()).resize(300, 300).config(Bitmap.Config.RGB_565).into(holder.iv_tupian_content);
        holder.tv_tupian_time.setText(datalists.get(position).getCreate_time().substring(0, 10));
        if (recyclerViewItemClickListener != null) {
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int clickposition = holder.getLayoutPosition() - 1;
                    recyclerViewItemClickListener.RecyclerViewItemClickListener(clickposition);
                }
            });
        }
        if (recyclerViewItemLongClickListener != null) {
            holder.itemView.setOnTouchListener(new View.OnTouchListener() {
                @Override
                public boolean onTouch(View v, MotionEvent event) {
                    if (event.getAction() == MotionEvent.ACTION_UP) {
                        int clickposition = holder.getLayoutPosition() - 1;
                        recyclerViewItemLongClickListener.RecyclerViewItemLongClickListener(clickposition);
                    }
                    return true;
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return datalists.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        private TextView tv_tupian_title;// 标题
        private ImageView iv_tupian_content;// 图片
        private TextView tv_tupian_time;// 发布时间

        public ViewHolder(View itemView) {
            super(itemView);
            tv_tupian_title = (TextView) itemView.findViewById(R.id.tv_tupian_title);// 标题
            iv_tupian_content = (ImageView) itemView.findViewById(R.id.iv_tupian_content);// 图片
            tv_tupian_time = (TextView) itemView.findViewById(R.id.tv_tupian_time);// 发布时间
        }
    }

}
